<template>
	<view :data-theme="theme">
		<view class='my-order'>
			<view class='header bg_color'>
				<view class='picTxt acea-row row-between-wrapper'>
					<view class='text'>
						<view class='name'>订单信息</view>
						<view>消费订单：{{orderData.orderCount || 0}}
							总消费：￥{{orderData.sumPrice ? Number(orderData.sumPrice).toFixed(2) : 0}}</view>
					</view>
					<view class='pictrue'>
						<image mode="widthFix"
							src='https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/orderTime.png'></image>
					</view>
				</view>
			</view>
			<view class='nav acea-row row-around'>
				<view class='item' :class='orderStatus==-1 ? "on": ""' @click="statusClick(-1)">
					<view>全部</view>
				</view>
				<view class='item' :class='orderStatus==0 ? "on": ""' @click="statusClick(0)">
					<view>待付款</view>
				</view>
				<view class='item' :class='orderStatus==1 ? "on": ""' @click="statusClick(1)">
					<view>待付尾款</view>
				</view>
				
				<view class='item' :class='orderStatus==2 ? "on": ""' @click="statusClick(2)">
					<view>待开营</view>
				</view>
				<view class='item' :class='orderStatus==3 ? "on": ""' @click="statusClick(3)">
					<view>待评价</view>
				</view>
				<view class='item' :class='orderStatus==4 ? "on": ""' @click="statusClick(4)">
					<view>已结营</view>
				</view>
			</view>
			<view class='list'>
				<view class='item' v-for="(item,index) in orderList" :key="index">
					<view @click='goOrderDetails(item.orderId)'>
						<view class='title acea-row row-between-wrapper'>
							<view class="acea-row row-middle">
								<view>{{item.createTime}}</view>
							</view>
							<view class='font_color'>{{item.orderStatus}}</view>
						</view>
						<view class='item-info  disp flex-row center row-top'
							v-for="(items,index) in item.orderInfoList" :key="index">
							<view class='pictrue'>
								<image :src='items.image'></image>
							</view>
							<view class='mid_right flex1 uni-ml-8'>
								<view class=" text acea-row row-between">
									<view class='name line2 flex1'>{{items.storeName}}</view>
									<view class='normal'>
										<view>￥{{items.price}}</view>
										<view>{{items.cartNum}}人报名</view>
									</view>
								</view>
								<view class="course_item disp flex-row between center uni-mt-6">
									<view class="course">课程</view>
									<view class="next one-line">{{items.course}}</view>
								</view>
								<!-- 营期SKU信息 -->
								<view class="course_item disp flex-row between center uni-mt-6">
									<view class="course">营期</view>
									<view class="next one-line">{{items.sku}}</view>
								</view>
								<view class="yingqi disp flex-row between center uni-mt-6">
									<view class="left_p disp flex-row center">
										<view class="date_info basic-label d4141 fs20 ">定金</view>
										<view class="money uni-ml-6">¥{{items.earnestMoney}}</view>
									</view>
									<view class="left_p disp flex-row center">
										<view class="date_info basic-label d4141 fs20">尾款</view>
										<view class="money uni-ml-6">¥{{items.lastMoney}}</view>
									</view>
								</view>

							</view>
						</view>

						<view class='totalPrice'>
							共{{item.totalNum}}个营地，总金额<text class='money'>￥{{item.payPrice}}</text>
						</view>
					</view>
					<view class='bottom acea-row row-right row-middle'>
						<view class='bnt' v-if="!item.paid" @click='cancelOrder(index,item.id)'>取消订单</view>
						<view class='bnt' v-if="!item.paid" @click='goPay(item.payPrice,item.orderId)'>立即付款</view>
						<view class='bnt' v-else-if="item.status== 0"@click='goPay(item.lastMoney,item.orderId)'>支付尾款</view>
						<view class='bnt' v-else-if="item.status== 1 && !item.studentFillAll"@click='goOrderDetails(item.orderId)'>填写学员</view>
						<view class='bnt bg_color' v-else-if="orderInfo.status== 1 && item.studentFillAll" @click='signAgree(orderInfo.orderId)'>签署协议</view>
						<view class='bnt'  @click='goOrderDetails(item.orderId)'>查看详情</view>
						<view class='bnt'
							v-if="item.paid === true && item.refundStatus === 0"
							@click='applyOrderRefund(item.orderId)'>申请售后</view>
						<view class='bnt' v-else-if="item.status==2" @click='goOrderDetails(item.orderId,0)'>
							去评价
						</view>
						<view class='bnt' v-if="item.status == 3" @click='delOrder(item.id,index)'>删除订单</view>
					</view>
				</view>
			
			</view>

			<view class='loadingicon acea-row row-center-wrapper'>
				<text class='loading iconfont icon-jiazai'
					:hidden='loading==false'></text>{{orderList.length>0?loadTitle:''}}
			</view>
			<view class='noCart' v-if="orderList.length == 0 && isShow && !loading">
				<view class='pictrue'>
					<image src='https://lives-1308318918.cos.ap-shanghai.myqcloud.com/images/noOrder.png'></image>
				</view>
			</view>
		</view>
		<payment :payMode='payMode' :pay_close="pay_close" @onChangeFun='onChangeFun' :order_id="pay_order_id"
			:totalPrice='totalPrice' ></payment>
	</view>
</template>

<script>
	import {
		getOrderList,
		orderData,
		orderCancel,
		orderDel
	} from '@/api/order.js';
	import {
		openOrderSubscribe
	} from '@/utils/SubscribeMessage.js';
	import payment from '@/components/payment';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	import emptyPage from '@/components/emptyPage.vue'
	import {
		setThemeColor
	} from '@/utils/setTheme.js'
	import animationType from '@/utils/animationType.js'
	const app = getApp();
	export default {
		components: {
			payment,
			emptyPage,
		},
		data() {
			return {
				loading: false, //是否加载中
				loadend: false, //是否加载完毕
				loadTitle: '加载更多', //提示语
				orderList: [], //订单数组
				orderData: {}, //订单详细统计
				orderStatus: 0, //订单状态
				page: 1,
				limit: 20,
				payMode: [{
					name: "微信支付",
					icon: "icon-weixinzhifu",
					value: 'weixin',
					title: '微信快捷支付',
					payStatus: 1,
				}],
				pay_close: false,
				pay_order_id: '',
				totalPrice: '0',
				isShow: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				theme: app.globalData.theme,
				bgColor: '#e93323'
			};
		},
		computed: mapGetters(['isLogin', 'userInfo']),
		onShow() {
			let that = this;
			that.bgColor = setThemeColor();
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: that.bgColor,
			});
			if (this.isLogin) {
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.getOrderData();
				this.getOrderList();
				//this.$set(this, 'payMode', this.payMode);
			} else {
				toLogin();
			}
		},
		methods: {
			getLeft(index){
				switch(index){
					case -1:
						return '0px'
					case 1:
					return '30rpx'
					default:
					return '15rpx'
					 
				}
			},
			onLoadFun() {
				this.getOrderData();
				this.getOrderList();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
				
			},
			/**
			 * 事件回调
			 * 
			 */
			onChangeFun: function(e) {
				let opt = e;
				let action = opt.action || null;
				let value = opt.value != undefined ? opt.value : null;
				(action && this[action]) && this[action](value);
			},
			/**
			 * 关闭支付组件
			 * 
			 */
			payClose: function() {
				this.pay_close = false;
			},
			/**
			 * 生命周期函数--监听页面加载
			 */
			onLoad: function(options) {
				if (options.status) this.orderStatus = options.status;
			},
			/**
			 * 获取订单统计数据
			 * 
			 */
			getOrderData: function() {
				let that = this;
				orderData().then(res => {
					that.$set(that, 'orderData', res.data);
				})
			},
			/**
			 * 取消订单
			 * 
			 */
			cancelOrder: function(index, order_id) {
				let that = this;
				if (!order_id) return that.$util.Tips({
					title: '缺少订单号无法取消订单'
				});
				uni.showModal({
					content: '确定取消该订单',
					cancelText: "取消",
					confirmText: "确定",
					showCancel: true,
					confirmColor: '#f55850',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '正在取消中'
							});
							orderCancel(order_id).then(res => {
								uni.hideLoading();
								return that.$util.Tips({
									title: '取消成功',
									icon: 'success'
								}, function() {
									that.orderList.splice(index, 1);
									that.$set(that, 'orderList', that.orderList);
									that.$set(that.orderData, 'unpaid_count', that
										.orderData.unpaid_count - 1);
									that.getOrderData();
								});
							}).catch(err => {
								return that.$util.Tips({
									title: err
								});
							});
						} else {

						}
					},
				})
			},
			/**
			 * 打开支付组件
			 * 
			 */
			goPay(pay_price, order_id) {
				this.$set(this, 'pay_close', true);
				this.$set(this, 'pay_order_id', order_id);
				this.$set(this, 'totalPrice', pay_price);
			},
			
			/**
			 * 支付成功回调
			 * 
			 */
			pay_complete: function() {
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.$set(this, 'pay_close', false);
				this.getOrderData();
				this.getOrderList();
			},
			/**
			 * 支付失败回调
			 * 
			 */
			pay_fail: function() {
				this.pay_close = false;
			},
			/**
			 * 去订单详情
			 */
			goOrderDetails: function(order_id, status) {
				if (!order_id) return that.$util.Tips({
					title: '缺少订单号无法查看订单详情'
				});
				uni.navigateTo({
					url: '/pages/summer/order_detail/dlist?order_id=' + order_id
				})
			},
			
			applyOrderRefund (order_id) {
				uni.navigateTo({
						url: '/pages/users/goods_return/index?orderId=' + order_id
				})
			},
			
			/**
			 * 切换类型
			 */
			statusClick: function(status) {
				if (status == this.orderStatus) return;
				this.orderStatus = status;
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.getOrderList();
			},
			/**
			 * 获取订单列表
			 */
			getOrderList: function() {
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				that.loading = true;
				that.loadTitle = "加载更多";
				getOrderList({
					type: that.orderStatus,
					page: that.page,
					limit: that.limit,
				}).then(res => {
					let list = res.data.list || [];
					let loadend = list.length < that.limit;
					that.orderList = that.$util.SplitArray(list, that.orderList);
					that.$set(that, 'orderList', that.orderList);
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? "我也是有底线的" : '加载更多';
					that.page = that.page + 1;
					that.isShow = true;
				}).catch(err => {
					that.loading = false;
					that.loadTitle = "加载更多";
				})
			},

			/**
			 * 删除订单
			 */
			delOrder: function(order_id, index) {
				uni.showModal({
					content: '确定删除该订单',
					cancelText: "取消",
					confirmText: "确定",
					showCancel: true,
					confirmColor: '#f55850',
					success: (res) => {
						if (res.confirm) {
							let that = this;
							orderDel(order_id).then(res => {
								that.orderList.splice(index, 1);
								that.$set(that, 'orderList', that.orderList);
								that.$set(that.orderData, 'unpaid_count', that.orderData
									.unpaid_count - 1);
								that.getOrderData();
								return that.$util.Tips({
									title: '删除成功',
									icon: 'success'
								});
							}).catch(err => {
								return that.$util.Tips({
									title: err
								});
							})
						} else {

						}
					},
				})
			},
		},
		onReachBottom: function() {
			this.getOrderList();
		}
	}
</script>

<style scoped lang="scss">
	.date_info {
		padding: 0 8rpx;
		line-height: 32rpx !important;
		text-align: center;
		border-radius: 4rpx;
		border: solid 2rpx #414141;
	}

	.my-order .header {
		height: 250rpx;
		padding: 0 30rpx;
	}

	.bg_color {
		background: linear-gradient(105deg, #9fd998, #66bbb1 100%);
	}

	.my-order .header .picTxt {
		height: 190rpx;
	}

	.my-order .header .picTxt .text {
		color: rgba(255, 255, 255, 0.8);
		font-size: 26rpx;
		font-family: 'Guildford Pro';
	}

	.my-order .header .picTxt .text .name {
		font-size: 34rpx;
		font-weight: bold;
		color: #fff;
		margin-bottom: 20rpx;
	}

	.my-order .header .picTxt .pictrue {
		width: 112rpx;
	}

	.my-order .header .picTxt .pictrue image {
		width: 100%;
		height: 100%;
	}

	.my-order .nav {
		background-color: #fff;
		border-radius: 14rpx;
		margin: 0 auto;
	}

	.my-order .nav .item {
		text-align: center;
		font-size: 26rpx;
		color: #282828;
		padding: 26rpx 0;
	}

	.my-order .nav .item.on {
		font-weight: bold;
		position: relative;
	}

	.my-order .nav .item.on ::after {
		content: '';
		height:6rpx;
		background-color: #007E51;
		position: absolute;
		bottom: 2rpx;
		left: 0;
		right: 0
	}

	.my-order .nav .item .num {
		margin-top: 18rpx;
	}

	.my-order .list {
		width: 690rpx;
		margin: 14rpx auto 0 auto;
	}

	.my-order .list .item {
		background-color: #fff;
		border-radius: 14rpx;
		margin-bottom: 14rpx;
	}

	.my-order .list .item .title {
		height: 84rpx;
		padding: 0 24rpx;
		border-bottom: 1rpx solid #eee;
		font-size: 28rpx;
		color: #282828;
	}

	.my-order .list .item .title .sign {
		font-size: 24rpx;
		padding: 0 13rpx;
		height: 36rpx;
		margin-right: 15rpx;
		border-radius: 18rpx;
		@include coupons_border_color(theme);
		@include main_color(theme);
	}

	.my-order .list .item .item-info {
		padding: 0 24rpx;
		margin-top: 22rpx;
	}

	.my-order .list .item .item-info .pictrue {
		width: 200rpx;
		height: 200rpx;
	}

	.my-order .list .item .item-info .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 14rpx;
	}

	.my-order .list .item .item-info .text {
		font-size: 28rpx;
		color: #999;
	}

	.my-order .list .item .item-info .text .name {
		color: #282828;
	}

	.my-order .list .item .item-info .text .money {
		text-align: right;
	}

	.font_color {
		// @include main_color(theme);
		color: #FFAA5B;
	}

	.my-order .list .item .totalPrice {
		font-size: 26rpx;
		color: #282828;
		text-align: right;
		margin: 27rpx 0 0 30rpx;
		padding: 0 30rpx 30rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.money {
		font-size: 28rpx;
		font-weight: bold;
		@include price_color(theme);
	}

	.my-order .list .item .bottom {
		height: 107rpx;
		padding: 0 30rpx;
	}

	.my-order .list .item .bottom .bnt {
		width: 176rpx;
		height: 60rpx;
		text-align: center;
		line-height: 60rpx;
		// color: #fff;
		border-radius: 50rpx;
		font-size: 27rpx;
		border: 1rpx solid #007E51;
		color:#007E51;
	}

	.my-order .list .item .bottom .bnt.cancelBnt {
		border: 1rpx solid #ddd;
		color: #aaa;
	}

	.my-order .list .item .bottom .bnt~.bnt {
		margin-left: 17rpx;
	}

	.noCart {
		margin-top: 171rpx;
		padding-top: 0.1rpx;
	}

	.noCart .pictrue {
		width: 414rpx;
		height: 336rpx;
		margin: 78rpx auto 56rpx auto;
	}

	.noCart .pictrue image {
		width: 100%;
		height: 100%;
	}
	
	.next {
		max-width: 320rpx;
	}
</style>